
package com.eltonkent.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.eltonkent.R;
import com.eltonkent.db.CustomTemplateTable;
import com.eltonkent.ui.adapters.SimpleIconListAdapter;
import com.eltonkent.ui.containers.SimpleIconListContainer;

public class CustomTemplateListActivity extends ParentActivity
{

	private ListView list;
	private static final int CREATE_TEMPLATE = 102;

	private AdapterView.OnItemClickListener listClickListener = new AdapterView.OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			SimpleIconListContainer selectedItem = (SimpleIconListContainer) list.getAdapter()
					.getItem(arg2);
			Log.d("UI", "SELECTED item ->" + selectedItem);
			Intent intent = new Intent();
			CustomTemplateTable table = new CustomTemplateTable(getApplicationContext());
			intent.putParcelableArrayListExtra(ApplicationConstants.EXTRA_FIELD_CONTAINER, table
					.getEntry(selectedItem.getTemplateName()));
			intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE_ICON, selectedItem.getIconResource());
			intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE, selectedItem.getTemplateName());
			setResult(RESULT_OK, intent);
			removeTextOnlyFromPath(getResources().getString(R.string.custom_templates));
			finish();
		}
	};

	private SimpleIconListContainer container;

	private void closeActivity(int resultCode)
	{
		setResult(resultCode);
		removeTextOnlyFromPath(getResources().getString(R.string.custom_templates));
		finish();
	}

	private final Dialog getDeleteDialog()
	{
		final Dialog dialog = getCustomDialog(R.layout.dialog_deletefolder, R.string.template_delete);
		Button[] okCancel = getOKCancelButtons(dialog);
		Button btnok = okCancel[0];
		Button btnCancel = okCancel[1];
		btnok.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				CustomTemplateTable table = new CustomTemplateTable(getApplicationContext());
				table.deleteTemplate(container.getTemplateName());
				refreshList();
				dialog.dismiss();
			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				dialog.dismiss();

			}
		});
		return dialog;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode != RESULT_CANCELED)
		{
			Log.d("UI", "Custom template created!");
			refreshList();
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem menuItem)
	{
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) menuItem.getMenuInfo();
		container = (SimpleIconListContainer) list.getAdapter().getItem(menuInfo.position);
		switch(menuItem.getItemId())
		{
			case R.id.mnu_DeleteEntry:
				showDialog(ApplicationConstants.DIALOG_DELETE);
				break;
		}
		return false;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.customtemplatelistcontextmenu, menu);
	}

	@Override
	protected final Dialog onCreateDialog(int id)
	{
		switch(id)
		{
			case ApplicationConstants.DIALOG_DELETE:
				return getDeleteDialog();
			default:
				return super.onCreateDialog(id);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		inflateMenu(menu, R.menu.customtemplatemenu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			closeActivity(RESULT_CANCELED);
			return true;
		}
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
			/* Add new folder */
			case R.id.mnu_AddEntry:
				// showDialog(DLG_NEW_FOLDER);
				Intent intent = new Intent(getApplicationContext(), CreateCustomTemplateActivity.class);
				intent.putExtra(ApplicationConstants.EXTRA_CUSTOM_ICON, "icons/blank.png");
				startActivityForResult(intent, CREATE_TEMPLATE);
				return true;
		}
		return false;
	}

	@Override
	protected void onPrepareDialog(int id, final Dialog dialog)
	{
		switch(id)
		{
			case ApplicationConstants.DIALOG_DELETE:
				TextView dialogText = (TextView) dialog.findViewById(R.id.dialogText);
				String text = getResources().getString(R.string.template_delete) + " "
						+ container.getTemplateName();
				dialogText.setText(text);
				break;
		}
	}

	@Override
	public SimpleIconListContainer onRetainNonConfigurationInstance()
	{
		return container;
	}

	@Override
	protected void populateUI()
	{
		container = (SimpleIconListContainer) this.getLastNonConfigurationInstance();
		addToPath(getResources().getString(R.string.custom_templates));
		refreshList();
	}

	private void refreshList()
	{
		CustomTemplateTable table = new CustomTemplateTable(getApplicationContext());
		SimpleIconListAdapter adapter = new SimpleIconListAdapter(getApplicationPreferences(),
				getApplicationContext(), R.layout.listview_icon_singleline_templates, table.getAllEntries());
		list.setAdapter(adapter);
		setStatusText(adapter.getCount() + " " + getResources().getString(R.string.total_items));
	}

	@Override
	protected void setupUI(Bundle savedInstance)
	{
		setContentView(R.layout.activity_genericlist);
		list = (ListView) findViewById(R.id.folderList);
		list.setDividerHeight(0);
		list.setOnItemClickListener(listClickListener);
		registerForContextMenu(list);
	}
}
